<?php
/**
 * Загрузчик приложения
 * 
 * @category Phorm
 * @package Bootstrap
 * @name /Phorm/Bootstrap.php
 * @copyright phorm.ru
 * @since 19.12.2010
 * @author vladimir@kachalov.net
 */

class Phorm_Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

	/**
	 * Инициализация Requesta
	 * 
	 * @return Zend_Controller_Request_Http
	 */
	
	protected function _initRequest() {
		
        // Обеспечение инициализации фронт-контроллера
        $this->bootstrap('FrontController');
 
        $request = new Zend_Controller_Request_Http();
        $this->FrontController->setRequest($request);

        return $request;
        
    }

	
	/**
	 * Инициализация объекта View
	 * 
	 * @return Phorm_View
	 */
	
	protected function _initView() {
        
        // Обеспечение инициализации requesta
        $this->bootstrap('Request');

        // Инициализация Phorm_View
        
		$view = new Phorm_View(APPLICATION_PATH . '/templates', array("compile_dir" => APPLICATION_PATH . '/cache/Smarty'));
		
		$view -> addHelperPath('Phorm/View/Helper','Phorm_View_Helper');
		$view -> addHelperPath('Phorm/JQuery/View/Helper','Phorm_JQuery_View_Helper');
		$view -> addHelperPath('ZendX/JQuery/View/Helper','ZendX_JQuery_View_Helper');
		
		$view -> doctype('XHTML1_STRICT');
		
		$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
		$viewRenderer -> setView($view)
					  -> setViewBasePathSpec(APPLICATION_PATH . '/templates')
					  -> setViewScriptPathSpec(':module.tpl');
		
		Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
		
		// Отключаем рендеринг объекта View при AJAX-запросах
		//if($this->Request->isXmlHttpRequest()) {
			//Zend_Controller_Action_HelperBroker::removeHelper("viewRenderer");
		//}
		
		return $view;
		
	}
	
	
	/**
	 * Инициализация объекта Layout
	 * 
	 * @return Phorm_Layout
	 */
	
	protected function _initLayout() {
		
		$layout = new Zend_Layout();
		
		$layout->setLayoutPath(APPLICATION_PATH . '/templates');
		$layout->setViewSuffix('tpl');
		
		return $layout;
		
	}
	
	
	/**
	 * Инициализация маршрутизатора
	 * 
	 * @return Zend_Controller_Router_Route
	 */
	
	protected function _initRouter() {
		
		// Обеспечение инициализации пользовательской сессии
		$this->bootstrap('UserSession');
		
		// Обеспечение инициализации фронт-контроллера
		$this->bootstrap('FrontController');
		
		$router = $this->FrontController->getRouter();
		
		$router -> addRoute('default',new Phorm_Controller_Router_Route(':controller/:action/*',
					array('module'=>'default','controller'=>'index','action'=>'index')));
		
		$router -> addRoute('control',new Zend_Controller_Router_Route('control/:controller/:action/*',
					array('module'=>'control','controller'=>'index','action'=>'index')));

		return $router;
		
	}
	
	
	/**
	 * Инициализация навигации
	 * 
	 * @return array
	 */
	
	/*protected function _initNavigation() {

		$dir = APPLICATION_PATH . '/configs/navigation';
		
		$containers = array();
		
		$this->bootstrap("Usersession");

		$acl = new Zend_Acl();
		$role = $this->Usersession->UserData["roleid"];
		$acl->addRole(new Zend_Acl_Role($role));
		
		$sql = "SELECT DISTINCT module,controller FROM pw_mvc_resources";
		
		foreach ($this->Db->fetchAll($sql) as $row) {
			$acl -> add(new Zend_Acl_Resource($row["module"].":".$row["controller"]));
		}
		
		foreach ($this->Usersession->UserData["acl"]["mvc"] as $modulename=>$moduledata) {
			foreach ($moduledata as $controllername=>$controllerdata) {
				$acl -> allow($role,$modulename.":".$controllername,array_keys($controllerdata));
			}
		}
		
		Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($acl);
	    Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($role);
	    
	    
	    $navigation = $this->View->navigation();
		
		$handle = opendir($dir);
		
		while ($file = readdir($handle)) { 

			if(preg_match("#^([^\.]+)\.([^\.]+)\.xml$#iu",$file,$fileinfo)) {
				
				$container = new Zend_Navigation(new Zend_Config_Xml($dir.'/'.$file));
				
				$containers[$fileinfo[1]][$fileinfo[2]] = array(
					"menu" => $navigation->menu($container)->render(),
					"breadcrumbs" => $navigation->breadcrumbs($container)->render(),
				);
				
			}
			
		}
		
		closedir($handle);
		
		return $containers;
		
	}*/
	
}
